package com.wc.算法提高课.A第一章_动态规划.树形DP.树的中心;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/12 21:21
 * @description https://www.acwing.com/problem/content/1075/
 */
public class Main {
    // 思路就是找上下
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10010, M = N << 1, idx = 1, INF = 0x3f3f3f3f;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    // d1[i]表示i下降的最远距离, d2[i]表示次远距离, p[i]表示i最远距离的下方向, up[i]表示上走的最远距离
    static int[] d1 = new int[N], d2 = new int[N], p = new int[N], up = new int[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i < n; i++) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c);
            add(b, a, c);
        }

        dfsD(1, -1);
        dfsU(1, -1);

        int res = INF;
        for (int i = 1; i <= n; i++) res = Math.min(res, Math.max(up[i], d1[i]));
        out.println(res);
        out.flush();
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static int dfsD(int u, int fa) {
        d1[u] = d2[u] = -INF;
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == fa) continue;
            int d = dfsD(j, u) + w[i];
            if (d > d1[u]) {
                d2[u] = d1[u];
                d1[u] = d;
                p[u] = j;
            } else if (d > d2[u]) {
                d2[u] = d;
            }
        }
        if (d1[u] == -INF) d1[u] = d2[u] = 0;
        return d1[u];
    }

    static void dfsU(int u, int fa) {
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == fa) continue;
            // 检查这个最大的是不是从这条路走的
            if (p[u] == j) up[j] = Math.max(up[u], d2[u]) + w[i];
            else up[j] = Math.max(up[u], d1[u]) + w[i];
            dfsU(j, u);
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
